<template>
  <main>
    <input type="text" placeholder="输入城市名" v-model="inputValue" />
    <div class="showCities">
      <ul v-if="inputValue.length > 0">
        <li v-for="item in list" :key="item.adcode" @click="goSearch(item.adcode, item.name)">
          {{ item.name }}
        </li>
        <li v-if="list.length == 0">似乎没有找到你查找的城市</li>
      </ul>
    </div>
    <div class="list-comp" v-if="inputValue.length === 0">
      <ListComp />
    </div>
    <div v-if="weatherStore.cityList.length === 0" class="cityList">
      暂时没有保存过城市天气信息，请查询后点击右上角"+"号保存。
    </div>
    <div class="chart">
      <div class="title">近期天气</div>
    </div>
    <zujian />
  </main>
</template>

<script setup>
import { ref, watch } from 'vue'
import { getSearch } from '@/api/weather'
import zujian from '../components/zujian.vue'
import ListComp from '../components/ListComp.vue'
import { useRouter } from 'vue-router'
import { useWeatherStore } from '../stores/WeatherStore.js'
const weatherStore = useWeatherStore()
const router = useRouter()
const inputValue = ref('')
const list = ref([])
watch(inputValue, async () => {
  const res = await getSearch(inputValue.value)
  list.value = res.districts
})
const goSearch = (key, name) => {
  router.push(`/search/${key}?search=${name}`)
}
</script>

<style lang="scss" scoped>
main {
  width: 1300px;
  margin: 0 auto;
  margin-top: 20px;
  input {
    width: 100%;
    height: 50px;
    padding: 0 5px;
    background: none;
    border: none;
    border-bottom: 1px solid white;
    color: white;
    font-size: 16px;
    &::placeholder {
      color: #ccc;
      // font-size: 16px;
    }
    &:focus {
      outline: none;
      border-bottom: none;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    }
  }
  .showCities {
    display: flex;
    gap: 2px;
    flex-direction: column;
    margin: 10px 0 20px 0;
    ul {
      list-style: none;
      li {
        height: 50px;
        color: white;
        line-height: 50px;
        padding: 0 10px;
        background: #224d6e;
        cursor: pointer;
      }
    }
  }
  .prompt {
    width: 100%;
    height: 100px;
    font-size: 20px;
    font-weight: 600;
    color: white;
    line-height: 100px;
    text-align: center;
  }
  .chart {
    width: 100%;
    color: white;
    .title {
      margin: 10px 0;
    }
  }
  .cityList {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 50px;
    // background: #224d6e;
    color: white;
    font-size: 18px;
    font-weight: 600;
  }
}
</style>
